//
//  SearchViewController.m
//  reader
//
//  Created by mo on 2021/12/12.
//  Copyright © 2021 mo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SearchViewController.h"
#import "LSYReadViewController.h"
#import "LSYReadPageViewController.h"
#import "LSYReadUtilites.h"
#import "LSYReadModel.h"
#import "Masonry.h"

@interface SearchViewController ()
@property (nonatomic, strong) UIButton* button1;    //按钮1

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发现";
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"grayBack"]];
    [self button1];
}

- (void)handleTouch1:(UIButton *)button{
    NSLog(@"读取缓存");
    
    LSYReadPageViewController *pageView = [[LSYReadPageViewController alloc] init];
    pageView.modalPresentationStyle = 0;
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"mdjyml" withExtension:@"txt"];
//    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"每天懂一点好玩心理学"withExtension:@"epub"];
    pageView.resourceURL = fileURL;    //文件位置
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        pageView.model = [LSYReadModel getLocalModelWithURL:fileURL];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self presentViewController:pageView animated:YES completion:nil];
        });
    });
}

- (UIButton *)button1 {
    //按钮背景颜色
    UIColor *backColor = [UIColor colorWithRed:0/255.0 green:201/255.0 blue:87/255.0 alpha:1.0];
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    
    if (_button1 == nil) {
        _button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _button1.titleLabel.font = [UIFont systemFontOfSize:15];
        _button1.layer.cornerRadius = 10;
        [_button1 setTitle:@"加载" forState:UIControlStateNormal];
        [_button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_button1 setBackgroundColor:backColor];
        [_button1 addTarget:self action:@selector(handleTouch1:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_button1];
        //设置约束
        [_button1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(screenBounds.size.width / 5, screenBounds.size.height / 18));
            make.center.equalTo(self.view);
        }];
    }
    return _button1;
}

@end

